package com.hkts.beltweigher.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.sql.Date;

public class DateUtil {

    public static LocalDateTime convertLocalDateTime(String str){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(str, formatter);
        return localDateTime;
    }

    public static LocalDateTime convertDate(String str){
        LocalDate date = LocalDate.parse(str);
        LocalTime time = LocalTime.of(0, 0); // 默认时间设为午夜
        LocalDateTime localDateTime = LocalDateTime.of(date, time);
        return localDateTime;
    }

    public static String getStrDate(LocalDateTime localDateTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String strDate = localDateTime.format(formatter);
        return strDate;
    }

    public static String getNowDate(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String strDate = now.format(formatter);
        return strDate;
    }
    public static String getStrDateHH(LocalDateTime localDateTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String strDate = localDateTime.format(formatter);
        return strDate;
    }

    public static String getStrTime(LocalDateTime localDateTime){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        String strDate = localDateTime.format(formatter);
        return strDate;
    }

    public static boolean getMaxTime(String startTime){
        LocalTime time1 = LocalTime.parse(startTime);
        LocalTime time2 = LocalTime.parse("23:59:00");

        if (time1.compareTo(time2) >= 0) {
            return true;
        } else {
            return false;
        }
    }

    public static String getNextDay(String dateStr){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateStr);
        LocalDate nextDay = date.plusDays(1);
        return nextDay.format(formatter);
    }

    public static boolean getBetweenTime(LocalTime localTime){
        LocalTime startTime = LocalTime.of(8, 0, 0);
        LocalTime endTime = LocalTime.of(20, 0, 0);
        if (localTime.compareTo(startTime) >= 0 && localTime.compareTo(endTime) <= 0) {
           return true;
        } else {
            return false;
        }
    }

    public static boolean getBeforeEightTime(LocalTime localTime){
        LocalTime startTime = LocalTime.of(0, 0);
        LocalTime endTime = LocalTime.of(8, 0);

        if (localTime.isAfter(startTime) && localTime.isBefore(endTime)) {
            return true;
        } else {
            return false;
        }
    }

    public static LocalDateTime getBeforeEightDate(){
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);

        // 创建代表晚上8点的LocalTime对象
        LocalTime time = LocalTime.of(20, 0);

        // 结合前一天的日期和晚上8点的时间创建LocalDateTime对象
        LocalDateTime yesterdayEvening8 = LocalDateTime.of(yesterday, time);
        return yesterdayEvening8;
    }

    public static  LocalDateTime getEightOClock(){
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 获取当天的20点整
        LocalDateTime eightOClock = LocalDateTime.of(currentDateTime.toLocalDate(), LocalTime.of(8, 0, 0));
        System.out.println("8点整的时间: " + eightOClock);
        return eightOClock;
    }
    public static  LocalDateTime getTwentyOClock(){
        LocalDateTime currentDateTime = LocalDateTime.now();
        // 获取当天的20点整
        LocalDateTime twentyOClock = LocalDateTime.of(currentDateTime.toLocalDate(), LocalTime.of(20, 0, 0));
        System.out.println("20点整的时间: " + twentyOClock);
        return twentyOClock;
    }

    public static Date getYesterdayDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        java.util.Date time = calendar.getTime();
        java.sql.Date sqlDate = new java.sql.Date(time.getTime());
        return sqlDate;
    }

    public static Date getNowStatisticDate(){
        Calendar calendar = Calendar.getInstance();
        java.util.Date time = calendar.getTime();
        java.sql.Date sqlDate = new java.sql.Date(time.getTime());
        return sqlDate;
    }

    public static void main(String[] args) {
        String strDate = getStrDate(LocalDateTime.now());
        String strTime = getStrTime(LocalDateTime.now());
        System.out.println(strDate + strTime);
    }
}
